<template>
	<view class="container">
		<view class="nav-box df" :style="'padding-top:' + statusBarHeight + 'px' + ';background:rgba(255, 255, 255,' + navbarTrans + ');'">
			<view class="nav-back df" :style="'height:' + titleBarHeight + 'px;'" @click="navBack">
				<image :class="1 != navbarTrans && 'xwb'" :src="1 == navbarTrans ? '/static/img/z.png' : '/static/img/z1.png'" style="width:34rpx;height:34rpx"></image>
			</view>
			<view v-if="1 == navbarTrans" class="nav-title ohto">{{user_info.name}}</view>
		</view>
		<view class="user-box" :style="'padding-top:' + (statusBarHeight + titleBarHeight) + 'px'">
			<view class="user-bg"></view>
			<view class="user-img" style="z-index:-2">
				<lazy-load :src="user_info.avatar"></lazy-load>
			</view>
			<view class="user-top df">
				<view class="avatar">
					<lazy-load :src="user_info.avatar"></lazy-load>
				</view>
				<view :class="['btn', user_info.is_follow && 'active']" @click="followClick">{{user_info.is_follow ? "已关注" : "＋关注"}}</view>
			</view>
			<view class="user-name">{{user_info.name}}</view>
			<view v-if="user_info.intro" class="user-intro">
				<text user-select="true">{{user_info.intro}}</text>
			</view>
			<view class="user-tag df">
				<view v-if="2 != user_info.gender" class="tag-item df">
					<image :src="1 == user_info.gender ? '/static/img/nan.png' : '/static/img/nv.png'"></image>
				</view>
				<view v-if="user_info.age && '暂不展示' != user_info.age" class="tag-item df">{{user_info.age}}</view>
				<view class="tag-item df">IP属地：{{user_info.province}}</view>
			</view>
			<view class="user-num df">
				<view class="num-item df" @click="toFollow">
					<text class="t1">{{user_info.follow_count}}</text>
					<text>关注</text>
				</view>
				<view class="num-item df" @click="toFollow">
					<text class="t1">{{user_info.fans_count}}</text>
					<text>粉丝</text>
				</view>
				<!-- <view class="num-item df" @click="likePopupClick">
					<text class="t1">{{user_info.like_count_str}}</text>
					<text>获赞</text>
				</view> -->
			</view>
		</view>
		<view class="content-box">
			<view v-if="user_info.circle" class="block-box">
				<view class="block-title">{{user_info.name}} 加入的圈子</view>
				<scroll-view scroll-x="true" style="width:100%;white-space:nowrap">
					<view class="circle-box">
						<view v-for="(item, i) in user_info.circle" :key="i" class="circle-item df" :data-url="'note/circle?id=' + item.id" @click="navigateToFun">
							<image class="circle-avatar" :src="item.avatar" mode="aspectFill"></image>
							<view class="circle-name ohto">{{item.name}}</view>
						</view>
						<view style="flex-shrink:0;width:15rpx;height:15rpx"></view>
					</view>
				</scroll-view>
			</view>
			<view class="bar-box df" :style="'top:' + (statusBarHeight + titleBarHeight - 1) + 'px'">
				<view v-for="(item,i) in bararr" :key="i" class="bar-item df" @click="barClick" :data-i="i">
					<text :style="'color:' + (i == baridx ? '#000' : '#999') + ';font-size:' + (i == baridx ? '28rpx' : '26rpx')">{{item}}</text>
					<view :style="'opacity:' + (a == baridx ? 1 : 0)" class="bar-line"></view>
				</view>
			</view>
			<view class="heio df" :style="'height:' + (isFun || 'loading' == loadStatus ? '0px' : '60rpx')">
				<uni-load-more :showText="false" status='loading'></uni-load-more>
			</view>
			<view v-if="isEmpty" class="empty-box df">
				<image src="/static/img/inset/null.png" />
				<view class="e1">{{0 == baridx ? "暂无笔记内容" : " 暂无喜欢的内容"}}</view>
				<view class="e2">{{user_info.name}}{{0 == baridx ? " 还没有发布过" : " 还没有点赞"}}</view>
			</view>
			<view v-else-if="1 == baridx && 0 == user_info.privacy.like" class="empty-box df">
				<image src="/static/img/inset/nolike.png" />
				<view class="e1">点赞内容不可见</view>
				<view class="e2">该用户已将点赞设为私密</view>
			</view>
			<view v-else class="dynamic-box">
				<multigraph :list="list" :page="page"></multigraph>
				<uni-load-more :showText="false" v-if="loadStatus != 'no-more'" :status="loadStatus"></uni-load-more>
			</view>
		</view>
		<uni-popup ref="likePopup" type="top" >
			<view class="like-popup">
				<image class="like-img" src="/static/img/inset/like.png" mode="aspectFill"></image>
				<view class="like-content">
					<text>"</text>
					{{user_info.name}}
					<text>"</text>
					共获得 {{user_info.like_count}} 个赞
				</view>
				<view class="like-btn" @click="likePopupClick(!1)">确认</view>
			</view>
		</uni-popup>
		
		<!-- Tips Box -->
		<uni-popup ref="tipsPopup" type="top" mask-background-color="rgba(0, 0, 0, 0)">
			<view class="tips-box df" :style="{marginTop: statusBarHeight+44+'px'}">
				<view class="tips-item">{{tips_title}}</view>
			</view>
		</uni-popup>

	</view>
</template>

<script>
	const app = getApp();
	const api = require('@/config/api');
	const util = require('@/utils/util');
	import dynamicBox from '@/components/dynamic/box.vue';
	export default {
		components: {
			dynamicBox
		},
		data() {
			return {
				statusBarHeight: app.globalData.statusBarHeight,
				titleBarHeight: app.globalData.titleBarHeight,
				insetUrl: api.insetUrl,
				scrollTop: 0,
				navbarTrans: 0,
				user_info: {
					name: '...',
					fans: 0,
					follow: 0,
					gender: 0,
					province: '...',
				},
				circle_list: [],
				bararr: ['动态', '赞'],
				baridx: 0,
				list: [],
				page: 1,
				isEmpty: false,
				isFun: true,
				loadStatus: 'more',
				tips_title: '',
			}
		},
		async onLoad(option) {
			util.showShareMenu();
			await this.$onLaunched;
			let that = this;
			if (option.id && option.id > 0) {
				that.id = option.id;
				that.userDetails();
				that.userCircle();
				that.userPublishContent();
			} else {
				that.tips_title = '未找到用户或用户异常！';
				that.$refs.tipsPopup.open();
				setTimeout(function() {
					that.$refs.tipsPopup.close();
					that.navBack();
				}, 2000);
			}
		},
		methods: {
			userDetails() {
				let that = this;
				util.request(api.userDetailsUrl, {
					user_id: that.id
				}).then(function(res) {
					if (res.code == 200) {
						that.user_info = res.data;
						
					} else {
						that.tips_title = res.msg;
						that.$refs.tipsPopup.open();
						setTimeout(function() {
							that.$refs.tipsPopup.close();
						}, 2000);
					}
				});
			},
			userCircle() {
				let that = this;
				util.request(api.userCircleUrl, {
					user_id: that.id
				}).then(function(res) {
					that.circle_list = res.data;
				});
			},
			userPublishContent() {
				let that = this;
				that.loadStatus = 'loading';
				that.isEmpty = false;
				util.request(api.userPublishContentUrl, {
					type: that.baridx,
					user_id: that.id,
					page: that.page
				}).then(function(res) {
					that.isFun = true;
					if (res.data && res.data.data.length) {
						that.list = that.list.concat(res.data.data);
						that.page = res.data.current_page;
						that.loadStatus = 'more';
					} else if (that.page == 1) {
						that.isEmpty = true;
						that.loadStatus = 'more';
					} else {
						that.loadStatus = 'no-more';
					}
				});
			},
			followUser() {
				let that = this;
				util.request(api.followUserUrl, {
					uname: uni.getStorageSync('user_info').name,
					user_id: that.user_info.id,
					is_follow: that.user_info.is_follow
				}, 'POST').then(function(res) {
					if (res.code == 200) {
						that.user_info.is_follow = !that.user_info.is_follow;
					} else {
						that.tips_title = res.msg;
						that.$refs.tipsPopup.open();
						setTimeout(function() {
							that.$refs.tipsPopup.close();
						}, 1500);
					}
				});
			},
			barClick(e) {
				if (this.isFun) {
					this.baridx = e.currentTarget.dataset.i;
					this.isFun = false;
					this.list = [];
					this.page = 1;
					this.userPublishContent();
				}
			},
			likeclick(e) {
				this.list[e.idx].is_like = e.is_like;
				this.list[e.idx].like_count = e.like_count;
			},
			navigateToFun(e) {
				let url = e.currentTarget.dataset.url;
				uni.navigateTo({
					url: '/pages/' + url
				})
			},
			navBack() {
				let currentRoutes = getCurrentPages();
				if (currentRoutes.length > 1) {
					uni.navigateBack();
				} else {
					uni.switchTab({
						url: '/pages/tabbar/index'
					})
				}
			},
			likePopupClick(e) {
				e || this.$refs.likePopup.close();
				e && this.$refs.likePopup.open();
			},
		},
		onReachBottom() {
			if (this.list.length) {
				this.page = this.page + 1;
				this.userPublishContent();
			}
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop;
			let frontColor = "#ffffff";
			var trans = (e.scrollTop > 200 ? 200 : e.scrollTop) / 200;
			if (trans >= 1) {
				frontColor = "#000000";
			}
			this.navbarTrans = trans;
			uni.setNavigationBarColor({
				frontColor: frontColor,
				backgroundColor: "#ffffff",
				animation: {
					duration: 400,
					timingFunc: 'easeIn'
				}
			})
		},
		onShareAppMessage: function() {},
		onShareTimeline(res) {
			return {
				title: app.globalData.shareTitle
			}
		},
	}
</script>

<style>
	@import url("/static/css/user/details.css");
</style>